package com.small.flowable.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * token传递
 *
 * @author wangguochao
 */
@Configuration
@Slf4j
public class FeignConfig implements RequestInterceptor {
    @Autowired
    private ClientCredentials clientCredentials;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            String tokenB = request.getHeader(HttpHeaders.AUTHORIZATION);
            log.info("测试", tokenB);
            if (tokenB == null) {
                tokenB = "Bearer " + clientCredentials.getToken();
            }
            log.info("测试0004004", tokenB);
            requestTemplate.header(HttpHeaders.AUTHORIZATION, tokenB);
        }
    }
}
